-- =============================================
-- Create a function to set default for CTHOADON
-- =============================================
USE DAILY
GO

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES 
   WHERE SPECIFIC_SCHEMA = N'dbo'
     AND SPECIFIC_NAME = N'DEF_CTHOADON' 
)
   DROP PROCEDURE dbo.DEF_CTHOADON
GO

CREATE PROCEDURE dbo.DEF_CTHOADON
	@id char(10)
AS
	DECLARE @MaMatHang char(10)
	DECLARE @SoLuong smallint
	SELECT @MaMatHang = MaMatHang, @SoLuong = SoLuong
		FROM dbo.CTHOADON 
		WHERE MaChiTiet = @id

	DECLARE @DonGia int
	EXEC PROC_GIANIEMYET @MaMatHang, @DonGia OUTPUT

	DECLARE @ChietKhau float
	EXEC PROC_TLCHIETKHAU @MaMatHang, @SoLuong, @ChietKhau OUTPUT

	UPDATE dbo.CTHOADON
		SET DonGia = @DonGia,
			ChietKhau = @ChietKhau
		WHERE MaChiTiet = @id
GO

-- =============================================
-- Create a function to set default for CTPHIEUNHAP
-- =============================================
USE DAILY
GO

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES 
   WHERE SPECIFIC_SCHEMA = N'dbo'
     AND SPECIFIC_NAME = N'DEF_CTPHIEUNHAP' 
)
   DROP PROCEDURE dbo.DEF_CTPHIEUNHAP
GO

CREATE PROCEDURE dbo.DEF_CTPHIEUNHAP
	@id char(10)
AS
	UPDATE dbo.CTPHIEUNHAP
		SET SoLuongTon = SoLuongNhap
		WHERE MaChiTiet = @id
GO

-- =============================================
-- Create a function to set default for LOAIMATHANG
-- =============================================
USE DAILY
GO

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES 
   WHERE SPECIFIC_SCHEMA = N'dbo'
     AND SPECIFIC_NAME = N'DEF_LOAIMATHANG' 
)
   DROP PROCEDURE dbo.DEF_LOAIMATHANG
GO

CREATE PROCEDURE dbo.DEF_LOAIMATHANG
	@id char(10)
AS
	UPDATE dbo.LOAIMATHANG
		SET TrangThai = 1
		WHERE MaLoai = @id
GO

-- =============================================
-- Create a function to set default for LUONGNHANVIEN
-- =============================================
USE DAILY
GO

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES 
   WHERE SPECIFIC_SCHEMA = N'dbo'
     AND SPECIFIC_NAME = N'DEF_LUONGNV' 
)
   DROP PROCEDURE dbo.DEF_LUONGNV
GO

CREATE PROCEDURE dbo.DEF_LUONGNV
	@id char(10)
AS
	DECLARE @MaNV char(10)
	SELECT @MaNV = MaNhanVien
		FROM dbo.LUONGNV
		WHERE MaLuong = @id

	DECLARE @Luong int
	EXEC dbo.PROC_LUONGNV @MaNV, @Luong OUTPUT
			
	UPDATE dbo.LUONGNV
		SET Luong = @Luong
		WHERE MaLuong = @id
GO

-- =============================================
-- Create a function to set default for MATHANG
-- =============================================
USE DAILY
GO

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES 
   WHERE SPECIFIC_SCHEMA = N'dbo'
     AND SPECIFIC_NAME = N'DEF_MATHANG' 
)
   DROP PROCEDURE dbo.DEF_MATHANG
GO

CREATE PROCEDURE dbo.DEF_MATHANG
	@id char(10)
AS
	UPDATE dbo.MATHANG
		SET TrangThai = 1
		WHERE MaMatHang = @id
GO

-- =============================================
-- Create a function to set default for NHANVIEN
-- =============================================
USE DAILY
GO

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES 
   WHERE SPECIFIC_SCHEMA = N'dbo'
     AND SPECIFIC_NAME = N'DEF_NHANVIEN' 
)
   DROP PROCEDURE dbo.DEF_NHANVIEN
GO

CREATE PROCEDURE dbo.DEF_NHANVIEN
	@id char(10)
AS
	UPDATE dbo.NHANVIEN
		SET TrangThai = 1
		WHERE MaNhanVien = @id
GO

-- =============================================
-- Create a function to set default for TKNHANVIEN
-- =============================================
USE DAILY
GO

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES 
   WHERE SPECIFIC_SCHEMA = N'dbo'
     AND SPECIFIC_NAME = N'DEF_TKNHANVIEN' 
)
   DROP PROCEDURE dbo.DEF_TKNHANVIEN
GO

CREATE PROCEDURE dbo.DEF_TKNHANVIEN
	@id char(10)
AS
	DECLARE @MaNV char(10)
	SELECT @MaNV = MaNhanVien 
		FROM dbo.TKNHANVIEN 
		WHERE MaTaiKhoan = @id

	DECLARE @NamSinh int
	SELECT @NamSinh = MONTH(NgaySinh) 
		FROM dbo.NHANVIEN 
		WHERE MaNhanVien = @MaNV

	UPDATE dbo.TKNHANVIEN
		SET TenDangNhap = 'Emp' + RIGHT(@MaNV, 8),
			MatKhau = TenDangNhap + RIGHT(@NamSinh, 2),
			TrangThai = 1
		WHERE MaTaiKhoan = @id
GO